package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.example.erp.domain.Customer;
import org.example.erp.domain.Units;
import org.example.erp.service.impl.CustomerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CustomerController {
    @Autowired
    private CustomerServiceImpl customerService;

    @GetMapping("/customerManagement/find")
    public DataResult find(@RequestParam(value = "currentPage", defaultValue = "1")Integer currentPage,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(required = false) String customerCode,
                           @RequestParam(required = false) String customerAbbreviation,
                           @RequestParam(required = false) String customerName,
                           @RequestParam(required = false) String status
                           ){
        LambdaQueryWrapper<Customer> customerWrapper = new LambdaQueryWrapper<>();
        IPage<Customer> page = new Page<>(currentPage, pageSize);
        customerWrapper
                .eq(!"".equals(customerCode) && customerCode!=null,Customer::getCustomerCode,customerCode)
                .eq(!"".equals(customerAbbreviation) && customerAbbreviation!=null,Customer::getCustomerAbbreviation,customerAbbreviation)
                .eq(!"".equals(customerName) && customerName!=null,Customer::getCustomerName,customerName)
                .eq(!"".equals(status) && status!=null,Customer::getStatus,status);
        IPage<Customer> pageResult = customerService.page(page, customerWrapper);
        return DataResult.success(pageResult);
    }


    @PostMapping("/customerManagement/insert")
    public DataResult insertcustomer(@RequestBody Customer customer){
        System.out.println("添加客户");
        customerService.save(customer);
        return DataResult.success();
    }


    @PutMapping("/customerManagement/update")
    public DataResult updatecustomer(@RequestBody Customer customer){
        customerService.updateById(customer);
        return DataResult.success();
    }


    @DeleteMapping("/customerManagement/delete")
    public DataResult deletecustomer(@RequestBody List<Integer> logIds) {
        customerService.removeByIds(logIds);
        return DataResult.success();
    }
}
